package rickyGame.model
{
	import flash.utils.Dictionary;
	
	import rickyGame.model.config.Ability;
	import rickyGame.model.config.Unit;

	public class ConfigUnitHelper
	{
		public function ConfigUnitHelper()
		{
			
		}
		
		/**
		 * 
		 * 初始化单位配置文档
		 */
		public static function initUnit(unitKey:String,data:String,keyID:String="unitID"):void
		{
			var arr:Dictionary=UnitKey.decompressionClassDic(data,keyID,Unit);
			cache[unitKey]=arr;
		}
		
		
		/**
		 * 
		 * 初始化配置文档
		 */
		public static function init(unitKey:String,data:String,className:Class,keyID:String="unitID"):void
		{
			var arr:Dictionary=UnitKey.decompressionClassDic(data,keyID,className);
			cache[unitKey]=arr;
		}
		
		
		/**
		 * 得到配置数据
		 */
		public static function getConfigByKey(key:String):Array
		{
			var r:Dictionary=cache[key];
			if(r)
			{
				var re:Array=[];
				
				for each(var item:* in r)
				re.push(item);
				
				return re;
			}
			else
				return null;
		}
		
		/**
		 * 得到配置数据
		 */
		public static function getDicConfigByKey(key:String):Dictionary
		{
			var r:Dictionary=cache[key];
			if(r)
				return r;
			else
				return null;
		}
		
		/**
		 * 得到单位配置
		 */
		public static function getUnitConfig(unitId:String):Unit
		{
			var unit:Unit=null;
			unit=getHeroConfig(unitId);
			if(!unit)
				unit=getBuildingConfig(unitId);
			if(!unit)
				unit=getGoodsConfig(unitId);
			return unit;
		}
		
		/**
		 * 得到英雄配置数据
		 */
		public static function getHeroConfig(unitId:String):Unit
		{
			var arr:Dictionary= cache[UnitKey.KeyHeroUnit];
			if(arr)
				return arr[unitId];
			return null;
		}
		
		/**
		 * 得到技能配置
		 */
		public static function getAbilityConfig(abilityID:String):Ability
		{
			var re:Dictionary= cache[UnitKey.KeyAbility];
			if(re)
				return re[abilityID];
			return null;
		}
		
		/**
		 * 得到建筑配置
		 */
		public static function getBuildingConfig(unitId:String):Unit
		{
			var re:Dictionary= cache[UnitKey.KeyBuildingUnit];
			if(re)
				return re[unitId];
			return null;
		}
		
		/**
		 * 得到物品配置
		 */
		public static function getGoodsConfig(unitID:String):Unit
		{
			var re:Dictionary= cache[UnitKey.KeyGoodsUnit];
			if(re)
				return re[unitID];
			return null;
		}
		
		private static var cache:Dictionary=new Dictionary();
	}
}